﻿using UnityGameFramework.Runtime;
using GameEntry = Fruits.Base.GameEntry;
using ProcedureOwner = GameFramework.Fsm.IFsm<GameFramework.Procedure.IProcedureManager>;

namespace Fruits.Procedure
{
    public class ProcedureInitResources : ProcedureBase
    {
        public override bool UseNativeDialog => true;
        
        /// <summary>
        /// 是否初始化资源完成
        /// </summary>
        private bool m_InitResourcesComplete;

        protected override void OnEnter(ProcedureOwner procedureOwner)
        {
            base.OnEnter(procedureOwner);
            
            m_InitResourcesComplete = false;
            //  PS: 使用单机模式并初始化资源，需要先构建AssetBundle并复制到StreamingAssets中，否则会产生HTTP 404错误
            GameEntry.Resource.InitResources(OnInitResourcesComplete);
        }

        protected override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
            if (!m_InitResourcesComplete)
                //  初始化资源未完成就继续等待
                return;
            //  进入预加载流程
            ChangeState<ProcedurePreload>(procedureOwner);
        }

        private void OnInitResourcesComplete()
        {
            m_InitResourcesComplete = true;
            Log.Info("Init resources complete.");
        }
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    }
}